package datas
{
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;

	public class FileManager
	{
		public static const OUTPUT_PATH_NAME: String 							= File.desktopDirectory.nativePath;
		public static const OUTPUT_ROOT_FOLDER_NAME: String 				= "JWebToon";
		public static const OUTPUT_IMAGES_FOLDER_NAME: String			= "images";
		public static const RESOURCE_FOLDER_NAME: String					= "_resource";
		
		public function FileManager()
		{
		}
		
		public static function clearRootPath(): void
		{
			var _file: File 	= new File( OUTPUT_PATH_NAME );
			_file 				= _file.resolvePath( OUTPUT_ROOT_FOLDER_NAME );
			if( _file.exists == true )
			{
				_file.deleteDirectory( true );
			}
		}
		
		private static function createOutputFolder(): File
		{
			var _file: File 	= new File( OUTPUT_PATH_NAME );
			
			_file 				= _file.resolvePath( OUTPUT_ROOT_FOLDER_NAME );
			if( _file.exists == false ) _file.createDirectory();
			
			return _file;
		}
		
		/**
		 * File 객체를 이미지 폴더 경로로 물리적 파일로 저장함 
		 * @param imageFile
		 * 
		 */		
		public static function saveToResourcePath( item: LibraryItem ): void
		{
			
			var _file: File		= FileManager.createOutputFolder();
			_file 					= _file.resolvePath( OUTPUT_IMAGES_FOLDER_NAME );
			if( _file.exists == false ) _file.createDirectory();
			
			// 파일 저장
			_file 					= _file.resolvePath( item.publishFileName );
			
			item.source.copyTo( _file, true );
			/*
			var _stream: FileStream			= new FileStream();
			_stream.open( _file, FileMode.WRITE );
			_stream.writeBytes( item.source.data, 0, item.source.data.length );
			_stream.close();
			*/
		}
		
		/**
		 * index.html 런타임에 필요한 각종 자원을 복사함 
		 * 
		 */		
		public static function copyToResource(): void
		{
			var _file: File 	= File.applicationDirectory;
			_file				= _file.resolvePath( RESOURCE_FOLDER_NAME );
			
			var _output: File	= FileManager.createOutputFolder();
			_file.copyTo( _output, true );
			
			// FLA 파일 제거
			_output.resolvePath("jdwebtoon/MusicPlayer.fla" ) .deleteFile();
			_output.resolvePath("jdwebtoon/SWFPlayerAS2.fla" ) .deleteFile();
			_output.resolvePath("jdwebtoon/SWFPlayerAS3.fla" ) .deleteFile();
			_output.resolvePath("jdwebtoon/VideoPlayer.fla" ) .deleteFile();
		}
		
		/**
		 * JSON 데이터를 파일로 생성
		 * @param tag
		 * 
		 */		
		public static function saveToIndexHTML( json: Array ): void
		{
			var _file: File 	= File.applicationDirectory;
			_file				= _file.resolvePath( RESOURCE_FOLDER_NAME );
			_file				= _file.resolvePath( "index.html" );
			
			var _stream: FileStream		= new FileStream();
			_stream.open(_file, FileMode.READ);
			var _htmlTemplate:String = _stream.readUTFBytes(_stream.bytesAvailable);
			_stream.close();
			
			_htmlTemplate = _htmlTemplate.replace( "[[CONTENT]]", "'["+json.join(",")+"]'" );
			
			// 생성
			_file				= FileManager.createOutputFolder();
			_file				= _file.resolvePath( "index.html" );
			_stream			= new FileStream();
			_stream.open( _file, FileMode.WRITE );
			_stream.writeUTFBytes( _htmlTemplate );
			_stream.close();
		}

	}
}